//
//  AppointmentToday.m
//  ICrEAM
//
//  Created by Cty on 2016/11/07.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AppointmentToday.h"
#import "ICMAppointmentTodayModel.h"
#import "AppointmentTodayCell.h"
@interface AppointmentToday()<UITableViewDelegate,UITableViewDataSource,ICMAppointmentTodayCellDelegate>
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic, assign) BOOL hasNext;
@property (nonatomic, assign) int pageNo;

@end
@implementation AppointmentToday

+ (instancetype)creatViewWithFrame:(CGRect)frame andModels:(NSArray *)dataSource{
    return [[AppointmentToday alloc]initWithFrame:frame andModels:dataSource];
}

- (instancetype)initWithFrame:(CGRect)frame andModels:(NSArray *)dataSource{
    
    self = [super initWithFrame:frame];
    
    if(self){
//        [self.dataSource addObjectsFromArray:dataSource];
        [self setupViews];
    }
    
    return self;
}

- (void)setupViews{
    [self addSubview:self.tableViewToday];
}

//上拉加载
//- (void)setRefresh {
//    __weak AppointmentToday *my = self;
//    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        if (self.hasNext) {
//            self.pageNo++;
//            [my loadMoreData];
//        } else {
//            [self.tableViewToday.mj_footer endRefreshingWithNoMoreData];
//        }
//    }];
//    
//    [footer setTitle:@"" forState:MJRefreshStateIdle];
//    [footer setTitle:@"正在加载" forState:MJRefreshStateRefreshing];
//    [footer setTitle:@"没有更多数据了" forState:MJRefreshStateNoMoreData];
//    self.tableViewToday.mj_footer = footer;
//}

//- (void)loadMoreData {
//    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
//    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//    [parameters setObject:self.condition forKey:@"QUERY_CONDITION"];
//    [parameters setObject:ICMEnumIntToString(ICMEnumAppointmentTypeToday) forKey:@"QUERY_TYPE"]; //1101当天  1102 未来计划
//    [parameters setObject:@"" forKey:@"QUERY_START_DATE"];
//    [parameters setObject:@"" forKey:@"QUERY_END_DATE"];
//    [parameters setObject:@(self.pageNo) forKey:@"CURRENT_PAGE"];
//    [manager startGetRequestWithURL:QueryBookingOrder parameters:parameters inHudView:self andResultBlock:^(id data, NSString *failText) {
//        if ([data[@"CODE"] integerValue] == 200) {
//            if (IsArr(data[@"TT_BOOKING_OEDER"])) {
//                NSArray *dataArray = data[@"TT_BOOKING_OEDER"];
//                if (dataArray.count != 0 && dataArray) {
//                    for (NSDictionary *searchData in dataArray) {
//                        ICMAppointmentTodayModel * model = [ICMAppointmentTodayModel yy_modelWithDictionary:searchData];
//                        [self.dataSource PACA_AddObject:model];
//                    }
//                    self.hasNext = YES;
//                    [self.tableViewToday.mj_footer endRefreshing];
//                } else {
//                    self.hasNext = NO;
//                    [self.tableViewToday.mj_footer endRefreshingWithNoMoreData];
//                }
//            } else {
//                self.hasNext = NO;
//                [self.tableViewToday.mj_footer endRefreshingWithNoMoreData];
//            }
//            [self.tableViewToday reloadData];
//        } else {
//            [self.tableViewToday.mj_footer endRefreshing];
//        }
//    }];
//}

/*tableViewDelegate tableViewDataSource*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ICMAppointmentTodayModel *model= [self.dataSource objectAtIndex:indexPath.row];
    if ([model.HAS_RECEPTION isEqualToString:@"1"]) {
        [self showString:@"该预约单已关联接车单，不能再编辑！" forSeconds:1.5];
        return;
    }
    if (![[NSString stringWithFormat:@"%@",model.status] isEqualToString:@"21141001"]) {
        return;
    }
    if([self.delegate respondsToSelector:@selector(didClickTodayCellWithIndex:)]){
        [self.delegate didClickTodayCellWithIndex:indexPath];
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AppointmentTodayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppointmentTodayCell"];
    if (!cell) {
        cell = [[AppointmentTodayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AppointmentTodayCell"];
    }
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell updateDataWithModel:[self.dataSource objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ActualHeight(95);
}

- (UITableView *)tableViewToday{
    if (!_tableViewToday) {
        _tableViewToday = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _tableViewToday.delegate = self;
        _tableViewToday.dataSource = self;
        _tableViewToday.backgroundColor = BackgroundColor_White;
        [_tableViewToday registerClass:[AppointmentTodayCell class] forCellReuseIdentifier:@"AppointmentTodayCell"];
        _tableViewToday.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        _tableViewToday.tableFooterView.backgroundColor = [UIColor whiteColor];
        _tableViewToday.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tableViewToday;
}

-(NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)reloadDataWithArray:(NSArray *)dataSource{
    [self.dataSource removeAllObjects];
    if (dataSource.count > 0) {
        self.pageNo = 1;
        self.hasNext = YES;
        //[self setRefresh];
    } else {
        self.tableViewToday.mj_footer.hidden = YES;
        [self showString:@"暂无相关搜索结果" forSeconds:1.5];
    }
    
    [self.dataSource addObjectsFromArray:dataSource];
    [self.tableViewToday reloadData];
}

- (void)didClickTodaygetCarWithIndex:(NSIndexPath *)index{
    if ([self.delegate respondsToSelector:@selector(didClickTodaygetCarWithIndex:)]) {
        [self.delegate didClickTodaygetCarWithIndex:index];
    }
    
}
- (void)didClickTodayPreViewWithIndex:(NSIndexPath *)index{
    if ([self.delegate respondsToSelector:@selector(didClickTodayPreViewWithIndex:)]) {
        [self.delegate didClickTodayPreViewWithIndex:index];
    }
}
@end
